(function ($) {
  $.fn.typer = function (options) {
    var defaults = $.extend(
      {
        search: '',
        replace: [],
        speed: 50,
        delay: 3000,
      },
      options
    );

    var bintext = function (length) {
      var text = '';
      for (var $i = 0; $i <= length; $i++) {
        text = text + Math.floor(Math.random() * 2);
      }
      return text;
    };

    this.each(function () {
      var $this = $(this);
      var $text = $this.data('text');
      var position = 0;

      var indexOf = $text.indexOf(defaults.search);
      var normal = $text.substr(0, indexOf);
      var changer = $text.substr(indexOf, $text.length);

      defaults.replace.push(changer);

      var interval = setInterval(function () {
        var $bintext = '';

        if (position == indexOf) {
          $bintext = bintext(changer.length - 1);

          $this.html($text.substr(0, normal.length));
          $this.append('<span>' + $bintext + '</span>');
        } else if (position > indexOf) {
          $bintext = bintext($text.length - 1);

          $this
            .delay(defaults.speed)
            .find('span')
            .html(
              changer.substring(0, position - indexOf) +
                $bintext.substring(position, $bintext.length)
            );
        } else if (position < indexOf) {
          $bintext = bintext($text.length - 1);

          $this
            .delay(defaults.speed)
            .html(
              normal.substring(0, position) +
                $bintext.substring(position, $bintext.length)
            );
        }

        if (position < $text.length) {
          position++;
        } else {
          clearInterval(interval);

          var index = 0;
          setInterval(function () {
            var position = 0;
            var newText = defaults.replace[index];

            var changeInterval = setInterval(function () {
              var $bintext = '';
              for (var $i = 0; $i <= newText.length - 1; $i++) {
                $bintext = $bintext + Math.floor(Math.random() * 2);
              }

              $this
                .delay(defaults.speed)
                .find('span')
                .html(
                  newText.substring(0, position) +
                    $bintext.substring(position, $bintext.length)
                );

              if (position < $text.length) {
                position++;
              } else {
                clearInterval(changeInterval);
              }
            }, defaults.speed);

            if (index < defaults.replace.length - 1) {
              index++;
            } else {
              index = 0;
            }
          }, defaults.delay);
        }
      }, defaults.speed);
    });
  };
  // 定时随机
  $.fn.fontShow = function (options) {};
})(jQuery);

$(function () {
  $('#textp_1').fontShow();

  $('#slogan').typer({
    search: 'CCustomize your ideal Al beings.',
    replace: [
      'With this "Prompt Workflows" design',
      'Imagine all possibilities: Companionships Industry or domain experts Personal Assistants',
      'st3333ranger',
    ],
  });
});
